<?php
class UAction extends FrontendAction
{
	public function __construct()
	{
		parent::__construct();
		$this->data['menu']	= 'user';
	}
	
	public function beforeExecute()
	{
		parent::beforeExecute();
		$this->user_id  = intval($this->context->get('uid'));
		if (!$this->user_id>0){
			if ($this->isLogin()){
				$this->user_id = $this->data['user']['id'];
			}
		}
		if ($this->isLogin() && $this->user_id == $this->data['user']['id']){
			$this->data['is_master'] = true;
			$this->data['master'] = $this->data['user'];
		}else{
			$userService = new UserService();
			$master = $userService->get($this->user_id);
			if (!$master){
				$this->redirect(C('LtUrl')->generate('Default','Error'));
			}
			$this->data['master'] = $master;
			$this->data['is_master'] = false;
			$followService = new FollowService();
			$this->data['is_follow'] = $followService->IsFollow($this->data['user']['id'], $this->user_id);
		}
		$this->data['master']['user_id'] = $this->user_id;
//		$this->data['master'] = array(
//					'user_id'=>$this->user_id,
//					'username'=>'淘蚁族',
//					'fans_num'=>100,
//					'follow_num'=>100,
//					'weibo_num'=>100,
//				);

	}
	
	
	protected function isFollow($user_id){
		static $myFollows = null;
		if (!$this->isLogin())
		{
			return false;
		}
		if ($user_id == $this->data['user']['id'])
		{
			return true;
		}
		if ($myFollows === null)
		{
			$followService = new FollowService();
			$myFollows = $followService->getFollowByUid($this->data['user']['id']);
			if($myFollows)
			{
				$tmp 		= $myFollows;
				$myFollows 	= array();
				foreach($tmp as $val)
				{
					$myFollows[$val['follow_id']] = true;
				}
				unset($tmp);
			}
		}
			//print_r($myFollows);exit;
		if (empty($myFollows))
		{
			return false;
		}
		
		if (isset($myFollows[$user_id]))
		{
			return true;
		}
	
		return false;
		
	}
	
	protected function isLogin(){
		if(isset($this->data['user']) && is_numeric($this->data['user']['id']) && $this->data['user']['id']>0)
		{
			return true;
		}
		return false;
	}
}
